package com.suse.controller;

import com.suse.dto.NoticeDto;
import com.suse.pojo.Notice;
import com.suse.service.NoticeService;
import com.suse.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
     NoticeService service;
    @GetMapping("/add")
    public ResultVo add(@Valid Notice n, BindingResult result) {
        if (result.hasErrors()){
            for(ObjectError error:result.getAllErrors()){
                String message = error.getDefaultMessage();
                return ResultVo.error(message);
            }
        }
        return service.add(n);
    }
    @PostMapping("/update")
    public ResultVo update(@RequestBody Notice n) {
        return service.update(n);
    }
    @GetMapping("/del")
    public ResultVo del(Integer id) {
        return service.del(id);
    }
    @GetMapping("/query")
    public ResultVo query(NoticeDto dto) {
        return service.select(dto);
    }
}